# dict
phonebook = {
    "alice": '123',
    "beth": "12132",
}

print(len(phonebook))  # 2

print(phonebook["alice"])  # 123

phonebook['alice'] = "999"
print(phonebook["alice"])  # 999

print('alice' in phonebook)  # True

del phonebook['alice']
print('alice' in phonebook)  # False

phonebook['wp'] = "888"
print(phonebook)  # {'beth': '12132', 'wp': '888'}

#
x = {}
x[32] = 'foo'
print(x)  # {32: 'foo'}

#
phonebook = {'Beth': '9102', 'Alice': '2341', 'Cecil': '3258'}
s = "Cecil's phone number is {Cecil}.".format_map(phonebook)
print(s)  # Cecil's phone number is 3258.
# #
# template = '''
# <html>
#  <head><title>{title}</title></head>
#  <body>
#     <p>{text}</p
#  <body>
# </html>
# '''
# data = {
#     "title": "Index Page",
#     "text": "from python"
# }
# print(template.format(data))

#
d = {}
d['name'] = 'Gum'
d['age'] = 32
print(d)  # {'name': 'Gum', 'age': 32}

# clear
ret_val = d.clear()
print(ret_val)  # None
print(d)  # {}

# copy
x = {'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
y = x.copy()
y['username'] = 'mlh'
print(x,
      y)  # {'username': 'admin', 'machines': ['foo', 'bar', 'baz']} {'username': 'mlh', 'machines': ['foo', 'bar', 'baz']}

# deepcoy
from copy import deepcopy

d = {}
d['name'] = [1, 2, 3]
c = d.copy()
dc = deepcopy(d)
print(d, c, dc)  # {'name': [1, 2, 3]} {'name': [1, 2, 3]} {'name': [1, 2, 3]}
d['name'].append(4)
print(d, c, dc)  # {'name': [1, 2, 3, 4]} {'name': [1, 2, 3, 4]} {'name': [1, 2, 3]}

# fromkeys 从key构建dict
d = {}.fromkeys(['name', 'age'])
print(d)  # {'name': None, 'age': None}

d = dict.fromkeys(['name', 'age'])
print(d)  # {'name': None, 'age': None}

d = dict.fromkeys(['name', 'age'], '(default)')
print(d)  # {'name': '(default)', 'age': '(default)'}

# get
d = {}
# print(d['name'])#KeyError: 'name'
print(d.get('name'))  # None
print(d.get('name', 'default'))  # default
d['name'] = 'Eric'
print(d.get('name', 'default'))  # Eric

# items
d = {'title': 'Python Web Site', 'url': 'http://www.python.org', 'spam': 0}
print(d.items())  # dict_items([('title', 'Python Web Site'), ('url', 'http://www.python.org'), ('spam', 0)])
for kv in d.items():
    print(kv)
# ('title', 'Python Web Site')
# ('url', 'http://www.python.org')
# ('spam', 0)

for key, val in d.items():
    print(key, val)
# title Python Web Site
# url http://www.python.org
# spam 0

# keys
print(d.keys(), type(d.keys()))  # dict_keys(['title', 'url', 'spam']) <class 'dict_keys'>

# pop 弹出指定key
d = {'x': 1, 'y': 2}
value = d.pop('x')
print(value)  # 1
print(d)  # {'y': 2}

# popitem 随机弹出一个kv
d = {'url': 'http://www.python.org', 'spam': 0, 'title': 'Python Web Site'}
print(d.popitem())  # ('title', 'Python Web Site')
print(d.popitem())  # ('spam', 0)

# setdefault
d = {}
d.setdefault("name", "default")
print(d)  # {'name': 'default'}
d.setdefault("name", "new default")
print(d)  # {'name': 'default'}

# update
d = {
    'title': 'web site',
    'url': "http://www.python.org",
    'changed': 'Mar 14'
}
x = {
    'title': "new python site",
    'time': "1990-09"
}

d.update(x)
print(d)  # {'title': 'new python site', 'url': 'http://www.python.org', 'changed': 'Mar 14', 'time': '1990-09'}

# values
d = {}
d[1] = 1
d[2] = 22
d[3] = 333
print(d)  # {1: 1, 2: 22, 3: 333}
print(d.values())  # dict_values([1, 22, 333])
